package com.fypg.module.medical.service.registerorderrefund;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.fypg.module.medical.controller.admin.registerorderrefund.vo.*;
import com.fypg.module.medical.dal.dataobject.outpatientorderdetail.registerorderrefund.RegisterOrderRefundDO;
import com.fypg.framework.common.pojo.PageResult;
import com.fypg.framework.common.util.object.BeanUtils;

import com.fypg.module.medical.dal.mysql.registerorderrefund.RegisterOrderRefundMapper;

import static com.fypg.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.fypg.framework.common.util.collection.CollectionUtils.convertList;
import static com.fypg.module.medical.enums.ErrorCodeConstants.*;

/**
 * 挂号订单退款信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class RegisterOrderRefundServiceImpl implements RegisterOrderRefundService {

    @Resource
    private RegisterOrderRefundMapper registerOrderRefundMapper;

    @Override
    public Long createRegisterOrderRefund(RegisterOrderRefundSaveReqVO createReqVO) {
        // 插入
        RegisterOrderRefundDO registerOrderRefund = BeanUtils.toBean(createReqVO, RegisterOrderRefundDO.class);
        registerOrderRefundMapper.insert(registerOrderRefund);
        // 返回
        return registerOrderRefund.getId();
    }

    @Override
    public void updateRegisterOrderRefund(RegisterOrderRefundSaveReqVO updateReqVO) {
        // 校验存在
        validateRegisterOrderRefundExists(updateReqVO.getId());
        // 更新
        RegisterOrderRefundDO updateObj = BeanUtils.toBean(updateReqVO, RegisterOrderRefundDO.class);
        registerOrderRefundMapper.updateById(updateObj);
    }

    @Override
    public void deleteRegisterOrderRefund(Long id) {
        // 校验存在
        validateRegisterOrderRefundExists(id);
        // 删除
        registerOrderRefundMapper.deleteById(id);
    }

    @Override
        public void deleteRegisterOrderRefundListByIds(List<Long> ids) {
        // 校验存在
        validateRegisterOrderRefundExists(ids);
        // 删除
        registerOrderRefundMapper.deleteByIds(ids);
        }

    private void validateRegisterOrderRefundExists(List<Long> ids) {
        List<RegisterOrderRefundDO> list = registerOrderRefundMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(REGISTER_ORDER_REFUND_NOT_EXISTS);
        }
    }

    private void validateRegisterOrderRefundExists(Long id) {
        if (registerOrderRefundMapper.selectById(id) == null) {
            throw exception(REGISTER_ORDER_REFUND_NOT_EXISTS);
        }
    }

    @Override
    public RegisterOrderRefundDO getRegisterOrderRefund(Long id) {
        return registerOrderRefundMapper.selectById(id);
    }

    @Override
    public PageResult<RegisterOrderRefundDO> getRegisterOrderRefundPage(RegisterOrderRefundPageReqVO pageReqVO) {
        return registerOrderRefundMapper.selectPage(pageReqVO);
    }

}
